<script>
	import http from './utils/http';
	export default {
		onLaunch: function(options) {
			if(options.query) {
			    let scene = options.query;
			    console.log("aaaaaaaaaaaaaaaaaaaaaaa",scene)
			    // 后续处理scene
				if(scene.shareId){
					wx.setStorage({
					  key:"shareId",
					  data:scene.shareId
					})
					this.globalData.shareId=scene.shareId
				}
				//小程序码进入
				if(options.query.scene) {
				    let scene = decodeURIComponent(options.query.scene); 
					this.globalData.shareId=scene.replace("shareId=","");
					wx.setStorage({
					  key:"shareId",
					  data:scene.replace("shareId=","")
					})
				    // 后续处理scene
				}
				console.log("this.globalData.shareId11111111111111111",this.globalData.shareId)
			}else{
				var this_=this
				wx.getStorage({
				  key: 'shareId',
				  success (res) {
					this_.globalData.shareId=res.data;
					console.log("ggggggggg",res.data)
				  }
				})
			}
			
			console.log("bbbbbbbbbbbbbbbbbbbb1111",this.globalData.shareId)
			console.log('App Launch')
			this.getUserInfo()
			this.getLocation()
			console.log('global', this.globalData)
			// const userInfoInterval = setInterval(() => {
			// 	this.getUserInfo();
			// }, 1 * 60 * 1000); // 每 5 分钟执行一次
			
			// const locationInterval = setInterval(() => {
			// 	this.getLocation();
			// }, 10 * 60 * 1000)
		},
		onShow: function() {
			console.log('App Show');  
		},
		onHide: function() {
			console.log('App Hide')
		},
		globalData: {
			openid: '', //openid
			// 用户信息
			user: {},
			// 是否获取位置授权
			isGetLocation: false,
			// 当前位置
			location: {

			},
			shopUserId:'',
			upLoadUrl:'https://love.lovein37.com/api/utils/upLoadFile',
			shareId:'',//分享人Id
			shareGoodsId:''
		},
		methods: {
			getUserInfo() {
				uni.login({
					"provider": "weixin",
					// "onlyAuthorize": true, // 微信登录仅请求授权认证
					success: (loginRes) => {
						//客户端成功获取授权临时票据（code）,向业务服务器发起登录请求。
						console.log("code", loginRes.code)
						http.request({
							url: '/api/user/get_user_info',
							method: 'POST',
							data: {
								code: loginRes.code
							}
						}).then((responseData) => {
							console.log("21321")
							this.globalData.user = responseData.data
							if (this.globalData.user.phone) {
								console.log("当前用户已存在数据库中:", this.globalData.user)
							} else {
								// uni.navigateTo({
								// 	url: "/pages/login/login"
								// });
							}
							console.log("global", this.globalData)
							//  这里应该还去判断用户的社区等信息是否存在，如果不存在可以来个提示绑定一下
						}).catch((err) => {
							// 在这里处理请求失败的情况
							console.log("err", err)
						});
					},
					fail: (ee) => {
						console.log("ee", ee)
					}
				})
			},
			// 获取位置授权和经纬度信息
			getLocation() {
				uni.getLocation({
					type: "wgs84",
					success: (res) => {
						console.log("1111111",res)
						this.globalData.isGetLocation = true;
						this.globalData.location.latitude = res.latitude
						this.globalData.location.longitude = res.longitude
						console.log(this.globalData.location)
					},
					fail: (err) => {
						console.log("errLocation", err)
						this.globalData.isGetLocation = false;
					}

				})
			}
		},
		// 初始加载用户信息和地理位置信息
		onMounted: function() {
			this.getUserInfo(); // 页面加载时先获取一次用户信息
			this.getLocation();
			每隔一定时间获取一次用户信息
			const userInfoInterval = setInterval(() => {
				getUserInfo();
			}, 1 * 60 * 1000); // 每 5 分钟执行一次

			const locationInterval = setInterval(() => {
				getLocation();
			}, 10 * 60 * 1000)
			// 组件销毁时清除定时器
			// onUnmounted(() => {
			// 	clearInterval(userInfoInterval);
			// 	clearInterval(locationInterval);
			// });
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>